package com.zhenyi.ayi.index;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import com.cenjiawen.common.IndexController;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Db;
import com.zhenyi.common.model.AyEmployer;
import com.zhenyi.common.model.AyMember;
import com.zhenyi.search.SearchUtilsPro;

import cn.hutool.core.date.DateUtil;

public class HomeController extends IndexController {

	@Override
	public void add() {
		renderSuccess();
	}

	@Override
	public void index() {

		// List<Activity> activities =
		// data.createQuery(Activity.class).limit(10).asList();
		// System.out.println("activities: " + activities.size());
		// Activity activity = new Activity();
		// activity.setTitle("act");
		// data.save(activity);
		/*
		 * List<Member> member =
		 * data.createQuery(Member.class).order("id").limit(5).asList();
		 * setAttr("member", member); render("/ayi/beikang.html");
		 */
		List<AyMember> list = AyMember.dao.find(Db.getSql("FindMember"));
		for (AyMember ayMember : list) {
			if (ayMember.getTags() != null) {
				String[] tags = ayMember.getTags().split(";");
				ayMember.put("tagList", tags);
			}
		}
		setAttr("member", list);
		render("/ayi/beikang.html");
	}

	public void info() {
		Long id = getParaToLong(0);
		AyMember member = AyMember.dao.findById(id);
		String[] images = member.getImages().split(";");
		member.put("imgs", images);
		setInfoItem(member);
		render("/ayi/info.html");
	}

	public void booking() {
		String id = getPara(0);
		setAttr("id", id);
		setAttr("today", DateUtil.today());
		render("/ayi/booking.html");
	}

	/**
	 * 页面用js 异步提交
	 */
	@Override
	public void submit() {
		String body = HttpKit.readData(getRequest());
		System.out.println(body);
		Long id = getParaToLong("id");
		String name = getPara("name");
		String mobile = getPara("mobile");
		String city = getPara("city");
		String date = getPara("date");
		AyEmployer employer = new AyEmployer();
		employer.setName(name);
		employer.setContact(mobile);
		employer.setCity(city);
		employer.setDate(date);
		employer.setMemberId(id);
		boolean flag = employer.save();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
		if (flag) {
			try {
				SearchUtilsPro searchUtils = new SearchUtilsPro("SearchEngine" + File.separatorChar + "beikang");
				HashMap<String, String> fields = new HashMap<>();
				String[] keys = { "no", "name", "idcard", "babyBirth", "city", "date", "source", "liveAddress", "type",
						"contact" };
				for (String key : keys) {
					if (key.equals("name"))
						continue;
					if (employer.get(key) != null && !employer.get(key).equals("")) {
						fields.put(key, employer.get(key));
					}
				}
				searchUtils.add(employer.getId(), employer.getName(), 2, null, fields);
			} catch (Exception e) {
				e.printStackTrace();
				renderFailure();
			}
			renderSuccess();
		} else {
			renderFailure();
		}

		// AySignInfo signInfo = new AySignInfo();
		// signInfo.setName(name);
		// signInfo.setMobile(mobile);
		// signInfo.setCity(city);
		// signInfo.setBookDate(date);
		// signInfo.setMemberId(id);
		// signInfo.save();

		// Record record = new Record();
		// record.setName(name);
		// record.setMobile(mobile);
		// record.setCity(city);
		// record.setMemberId(id);
		// record.setAddTime(new Date());
		// record.setDate(date);
		// Datastore data = MongoUtils.getInstance().getDB();
		// data.save(record);
	}

	public void result() {
		render("/ayi/result.html");
	}
}
